package com.huabo.monitor.controller;

import com.hbfk.util.JsonBean;
import com.huabo.monitor.oracle.entity.TblTestTemplate;
import com.huabo.monitor.oracle.entity.TblTestplan;
import com.huabo.monitor.service.TblTestElementService;
import com.huabo.monitor.service.TblTestPlanService;
import com.huabo.monitor.service.TblTestTempTypeService;
import com.huabo.monitor.service.TblTestTemplateService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping(value = "/nbkz")
public class NKcsController extends BaseController {

    @Resource
    public TblTestTempTypeService tblTestTempTypeService;

    @Resource
    public TblTestTemplateService tblTestTemplateService;

    @Resource
    public TblTestElementService tblTestElementService;

    @Resource
    public TblTestPlanService tblTestPlanService;

    /**
     * 内控测试模板列表
     *
     * @return
     */
    @GetMapping("/csmb/def_tmpl_list")
    @ApiOperation("测试模板")
    public JsonBean csfa_def_tmpl_list(
            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token,
            @ApiParam(name = "pageNumber", value = "分页当前页数", required = false) @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
            @ApiParam(name = "pageSize", value = "分页当前行数", required = false) @RequestParam(value = "pageSize", required = false, defaultValue = "1") Integer pageSize,
            @ApiParam(name = "templeNumber", value = "测试模板编号", required = false) @RequestParam(value = "templeNumber", required = false) Integer templeNumber,
            @ApiParam(name = "templename", value = "测试模板名字", required = false) @RequestParam(value = "templename", required = false) String templename) {
        JsonBean jsonBean = null;
        try {
            jsonBean = tblTestTemplateService.selectList(token, pageNumber, pageSize, templeNumber, templename);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonBean;
    }


    /**
     * 新建模板
     *
     * @return
     */
    @PostMapping(value = "/csmb/def_tmpl_add")
    @ApiOperation("新建模板")
    public JsonBean def_tmpl_add(@RequestBody TblTestTemplate template) {
        JsonBean jsonBean = null;
        try {
            jsonBean = tblTestTemplateService.add(template);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonBean;

    }

    /**
     * 模板修改
     *
     * @param testtemid
     * @return
     */
    @PostMapping(value = "/csmb/def_tmpl_update")
    @ApiOperation("模板修改")
    public JsonBean def_tmpl_upate(@ApiParam(name = "testtemid", value = "测试模板id", required = false) @RequestParam(value = "testtemid", required = false) BigDecimal testtemid) {
        JsonBean jsonBean = null;
        try {
            jsonBean = tblTestTemplateService.updateById(testtemid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonBean;
    }

    /**
     * 模板删除
     *
     * @param templeNumber
     * @return
     */
    @DeleteMapping(value = "/csmb/tempdelete")
    @ApiOperation("模板删除")
    public JsonBean tempdelete(@ApiParam(name = "templeNumber", value = "测试模板编号", required = false)
                                   @RequestParam(value = "templeNumber", required = false) String templeNumber) {
        JsonBean jsonBean = null;
        try {
            jsonBean = tblTestTemplateService.deleteById(templeNumber);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonBean;
    }

    /**
     * 保存 基本信息
     *
     * @param tblTestTemplate
     * @return
     */
    @RequestMapping(value = "/csmb/def_tmpl_save")
    @ApiOperation("保存信息")
    public JsonBean def_tmpl_save(@RequestBody TblTestTemplate tblTestTemplate) {
        JsonBean jsonBean = null;
        try {
            jsonBean = tblTestTemplateService.save(tblTestTemplate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonBean;
    }

    /**
     * 测试方案-计划查找
     * @param plannumber
     *  @param planname
     * @param planstatus
     *  @param starttime_min
     * @param starttime_max
     * @param pageNumber
     *  @param pageSize
     *
     * @return
     */
    @RequestMapping(value = "/nkcs/plan/ctrltest_plan_list")
    @ApiOperation("测试方案-计划查找")
    public JsonBean nkcs_ctrltest_plan_list(@ApiParam(name="plannumber",value = "计划编号",required = false)@RequestParam(value = "plannumber",required = false)String plannumber,
                                            @ApiParam(name="planname",value = "计划名称",required = false)@RequestParam(value = "planname",required = false)String planname,
                                            @ApiParam(name="planstatus",value = "计划状态",required = false)@RequestParam(value = "planstatus",required = false)String planstatus,
                                            @ApiParam(name="starttime_min",value = "最小开始时间",required = false)@RequestParam(value = "starttime_min",required = false) Date starttime_min,
                                            @ApiParam(name="starttime_max",value = "最大开始时间",required = false)@RequestParam(value = "starttime_max",required = false)Date starttime_max,
                                            @ApiParam(name = "pageNumber", value = "分页当前页数", required = false) @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
                                            @ApiParam(name = "pageSize", value = "分页当前行数", required = false) @RequestParam(value = "pageSize", required = false, defaultValue = "1") Integer pageSize) {
        JsonBean jsonBean = null;
        try {
            jsonBean = tblTestPlanService.selectList(plannumber, planname, planstatus, starttime_min, starttime_max, pageNumber, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonBean;
    }


    /**
     * 计划保存
     * @param tblTestplan
     * @return
     */
    @RequestMapping(value = "/nkcs/plan/saves")
    @ApiOperation("计划保存")
    public JsonBean nkcs_csjhSave(@RequestBody TblTestplan tblTestplan){
        JsonBean jsonBean = null;
        try {
            jsonBean = tblTestPlanService.save(tblTestplan);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonBean;
    }

    /**
     * 新增计划
     * @param tblTestplan
     * @return
     */
    @RequestMapping(value = "/nkcs/plan/add")
    @ApiOperation("新增计划")
    public JsonBean nkcs_ctrltest_plan_add(@RequestBody TblTestplan tblTestplan) {
        JsonBean jsonBean = null;
        try {
            jsonBean = tblTestPlanService.add(tblTestplan);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonBean;
    }

    /**
     * 计划删除
     * @param testplanid
     * @return
     */
    @RequestMapping(value = "/nkcs/plan/delete")
    @ApiOperation("计划删除")
    public JsonBean nkcs_ctrltest_plan_delete(@ApiParam(name="testplanid",value = "计划编号",required = false)@RequestParam(value = "testplanid",required = false)BigDecimal testplanid) {
        JsonBean jsonBean = null;
        try {
            jsonBean = tblTestPlanService.deleteById(testplanid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonBean;
    }

    /**
     * 计划修改/分配
     *
     * @param testplanid
     * @return
     */
    @RequestMapping(value = "/nkcs/plan/isStatus")
    @ApiOperation("计划修改/分配")
    public JsonBean zgfp_fqStatus(@ApiParam(name = "testplanid", value = "计划编号", required = false) @RequestParam(value = "testplanid", required = false) BigDecimal testplanid) {
        JsonBean jsonBean = null;
        try {
            jsonBean = tblTestPlanService.updateById(testplanid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonBean;
    }

    /**
     * 启动测试方案
     *
     * @param testplanid
     * @return
     */
    @RequestMapping(value = "/nkcs/plan/start_isStatus")
    @ApiOperation("启动测试方案")
    public JsonBean start_isStatus(@ApiParam(name="testplanid",value = "测试计划id",required = false)@RequestParam(value = "testplanid",required = false)BigDecimal testplanid) {
        JsonBean jsonBean = null;
        try {
            jsonBean = tblTestPlanService.updateById(testplanid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonBean;
    }

    /**
     * 测试任务--查看测试任务
     *
     * @param testplanid
     * @return
     */
    @RequestMapping(value = "/csrw/gettree")
    @ApiOperation("测试任务--查看测试任务")
    public JsonBean csrw_getTree(@ApiParam(name = "testplanid", value = "测试计划id", required = false) @RequestParam(value = "testplanid", required = false) BigDecimal testplanid) {
        JsonBean jsonBean = null;
        try {
            jsonBean = tblTestPlanService.findById(testplanid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonBean;
    }


    /**
     * 测试任务--修改
     * @param testplanid
     * @return
     */
    @RequestMapping(value = "/csrw/update")
    public JsonBean csrw_update(@ApiParam(name = "testplanid", value = "测试计划id", required = false) @RequestParam(value = "testplanid", required = false) BigDecimal testplanid) {
        JsonBean jsonBean = null;
        try {
            jsonBean = tblTestPlanService.update(testplanid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonBean;
    }

    /**
     * 测试任务--全部提交
     * @param plans
     * @return
     */
    @RequestMapping(value = "/csrw/saveAll")
    public JsonBean csrw_saveAll(List<String> plans){
        JsonBean jsonBean = null;
        try {
            jsonBean = tblTestPlanService.saveAll(plans);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonBean;
    }

    /**
     * 测试跟踪-查找
     *
     * @param plannumber
     * @param planname
     * @param planstatus
     * @param starttime_min
     * @param starttime_max
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/nkcs/impl/control_test_track_list")
    public JsonBean control_test_track_list(@ApiParam(name = "plannumber", value = "计划编号", required = false) @RequestParam(value = "plannumber", required = false) String plannumber,
                                            @ApiParam(name = "planname", value = "计划名称", required = false) @RequestParam(value = "planname", required = false) String planname,
                                            @ApiParam(name = "planstatus", value = "计划状态", required = false) @RequestParam(value = "planstatus", required = false) String planstatus,
                                            @ApiParam(name = "starttime_min", value = "最小开始时间", required = false) @RequestParam(value = "starttime_min", required = false) Date starttime_min,
                                            @ApiParam(name = "starttime_max", value = "最大开始时间", required = false) @RequestParam(value = "starttime_max", required = false) Date starttime_max,
                                            @ApiParam(name = "pageNumber", value = "分页当前页数", required = false) @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
                                            @ApiParam(name = "pageSize", value = "分页当前行数", required = false) @RequestParam(value = "pageSize", required = false, defaultValue = "1") Integer pageSize) {
        JsonBean jsonBean = null;
        try {
            jsonBean = tblTestPlanService.selectList(plannumber, planname, planstatus, starttime_min, starttime_max, pageNumber, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonBean;
    }


    @RequestMapping(value = "/nkcs/statistic/result_count_list")
    public JsonBean nkcs_result_count_list(@ApiParam(name = "planname", value = "计划名称", required = false) @RequestParam(value = "planname", required = false) String planname,
                                           @ApiParam(name = "planyear", value = "计划名称", required = false) @RequestParam(value = "planyear", required = false) String planyear,
                                           @ApiParam(name = "pageNumber", value = "分页当前页数", required = false) @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
                                           @ApiParam(name = "pageSize", value = "分页当前行数", required = false) @RequestParam(value = "pageSize", required = false, defaultValue = "1") Integer pageSize) {
        JsonBean jsonBean = null;
        try {
            jsonBean = tblTestPlanService.select(planname, planyear, pageNumber, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonBean;
    }

}
